package org.csu.goodpartner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.goodpartner.VO.AccountVO;
import org.csu.goodpartner.common.CommonResponse;
import org.csu.goodpartner.entity.Account;
import org.csu.goodpartner.entity.Class;
import org.csu.goodpartner.entity.Item;
import org.csu.goodpartner.entity.Shop;
import org.csu.goodpartner.entity.SignOn;
import org.csu.goodpartner.persistence.*;
import org.csu.goodpartner.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class AccountServiceImpl implements AccountService  {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SignOnMapper signOnMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderMapper ordersMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ClassMapper classMapper;
    @Override
    public CommonResponse<Account> registerAccount(String username, String password, String telephone) {
        //前端校验无误
        //用户名可重复
        //插入account表，同时返回account自带id
        Account account=new Account();
        account.setName(username);
        account.setPhone(telephone);
        account.setStatus(1);

        accountMapper.insert(account);

        System.out.println(account.getId());

        //插入signon表
        SignOn signOn=new SignOn();
        signOn.setPassword(password);
        signOn.setType(1);
        signOn.setUserid(account.getId());
        signOnMapper.insert(signOn);

        //返回值
        return CommonResponse.createForSuccess("插入成功",account);
    }

    //登录账号
    @Override
    public CommonResponse<AccountVO> login(int id, String password) {
        //检验id与password
        SignOn signOn=new SignOn();

        QueryWrapper<SignOn> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",id);
        queryWrapper.eq("password",password);
        signOn=signOnMapper.selectOne(queryWrapper);


        if(signOn!=null)
        {
            //查询对应account表
            int status=((Account)accountMapper.selectById(signOn.getUserid())).getStatus();
            if(status==1) {
                //查找对应account信息
                Account account = accountMapper.selectById(id);
                AccountVO accountVO = entityToVO(account, signOn);
                return CommonResponse.createForSuccess("登陆成功", accountVO);
            }
            else
            {
                return CommonResponse.createForError("您的账号已注销，请重新注册");
            }
        }
        else
        {
            return CommonResponse.createForError("用户名或密码错误");
        }

    }

    @Override
    public CommonResponse<AccountVO> updateAccount(int id, String username, String password, String telephone, String IDCard, String email, String addr) {

        //查询得到营业执照
       Account account= accountMapper.selectById(id);
        //更新account表

        account.setName(username);
        account.setAddr(addr);
        account.setPhone(telephone);
        account.setIDcard(IDCard);
        account.setEmail(email);
        account.setId(id);

        accountMapper.updateById(account);

        //更新signon表
        SignOn signOn=new SignOn();
        signOn.setPassword(password);
        signOn.setUserid(id);
        signOn.setType(1);

        signOnMapper.updateById(signOn);

        //转存
        AccountVO accountVO=entityToVO(account,signOn);

        return CommonResponse.createForSuccess("修改成功",accountVO);

    }

    @Override
    public CommonResponse deleteAccount(int id) {
        //根据id更改信息
        //账号注销
        UpdateWrapper<Account> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ownerid",id).set("status",0);
        accountMapper.update(null,updateWrapper);
//        //signon表更改
//        UpdateWrapper<SignOn> updateWrapper1=new UpdateWrapper<>();
//        updateWrapper1.eq("userid",id).set("status",0);
//        signOnMapper.update(null,updateWrapper1);
        //shop表更改
        UpdateWrapper<Shop> updateWrapper2=new UpdateWrapper<>();
        updateWrapper2.eq("ownerid",id).set("status",0);
        shopMapper.update(null,updateWrapper2);
        //item表更改
        //先查询表，得到对应的shopid
        QueryWrapper<Shop> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ownerid",id);
        List<Shop> shopList=new ArrayList<>();
        shopList=shopMapper.selectList(queryWrapper);
        //循环shopList表
        for(Shop shops:shopList)
        {
            UpdateWrapper<Item> updateWrapper3=new UpdateWrapper<>();
            updateWrapper3.eq("shopid",shops.getShopid()).set("status",0);
            itemMapper.update(null,updateWrapper3);
        }
        //更改class表
        //同理，进行循环遍历
        for(Shop shops:shopList)
        {
            UpdateWrapper<Class> updateWrapper4=new UpdateWrapper<>();
            updateWrapper4.eq("shopid",shops.getShopid()).set("status",0);
            classMapper.update(null,updateWrapper4);
        }

        //更改完成，账号注销完成
        return CommonResponse.createForSuccessMessage("账号注销成功");


    }

    @Override
    public CommonResponse<AccountVO> oneAccount(int id) {
        SignOn signOn=signOnMapper.selectById(id);
        Account account=accountMapper.selectById(id);
        AccountVO accountVO=entityToVO(account,signOn);
        return CommonResponse.createForSuccess("查询成功",accountVO);
    }

    private AccountVO entityToVO(Account account, SignOn signOn)
    {
       AccountVO accountVO=new AccountVO();
       accountVO.setAddr(account.getAddr());
       accountVO.setEmail(account.getEmail());
       accountVO.setLicense(account.getLicense());
       accountVO.setId(account.getId());
       accountVO.setIDcard(account.getIDcard());
       accountVO.setName(account.getName());
       accountVO.setPassword(signOn.getPassword());
       accountVO.setType(signOn.getType());
       accountVO.setPhone(account.getPhone());
       accountVO.setStatus(account.getStatus());

       return accountVO;
    }

    //注册账号

}
